package cn.icanci.ladybird.service.user.impl;

import cn.icanci.ladybird.pojo.dto.UserClassDetailsDto;
import cn.icanci.ladybird.pojo.entity.*;
import cn.icanci.ladybird.service.*;
import cn.icanci.ladybird.service.user.UserClassService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author: wirdeo
 * @date: Created in 2020/10/2 20:37
 * @classAction: 相关度排序
 */
@SuppressWarnings("all")
@Service
public class UserClassServiceImpl implements UserClassService {
    @Autowired
    private TicketDetailsService ticketDetailsService;
    @Autowired
    private TicketDetailsTicketAddressMiddleService ticketDetailsTicketAddressMiddleService;
    @Autowired
    private TicketAddressService ticketAddressService;
    @Autowired
    private TicketDetailsTicketPriceMiddleService ticketDetailsTicketPriceMiddleService;
    @Autowired
    private TicketPriceService ticketPriceService;
    @Autowired
    private TicketTimeService ticketTimeService;
    @Autowired
    private TicketDetailsTicketTimeMiddleService ticketDetailsTicketTimeMiddleService;
    @Autowired
    private UserClassService userClassService;

    @Override

    public List<UserClassDetailsDto>  userClassDetailsDtoList() {
        List<TicketDetails> ticketDetailsList = ticketDetailsService.queryManyByWeight(0, 6);
        //将所有要取出的数据封装到一个集合中
        ArrayList<UserClassDetailsDto> userClassDetailsDtos = new ArrayList<>();
        //遍历集合得到查到的票
        for (TicketDetails ticketDetails : ticketDetailsList) {
            //            //创建Dto对象
            UserClassDetailsDto userClassDetailsDto = new UserClassDetailsDto();
            //拿到id
            Long ticketDetailsId = ticketDetails.getTicketDetailsId();
            //设置Dto对象,id,商品名,图片地址
            userClassDetailsDto.setTicketDetailsId(ticketDetailsId);
            userClassDetailsDto.setTicketDetailsInfo(ticketDetails.getTicketDetailsInfo());
            userClassDetailsDto.setTicketDetailsTicketImgUrl(ticketDetails.getTicketDetailsTicketImgUrl());
            //城市
            userClassDetailsDto.setTicketAddressCity(ticketDetails.getTicketAddressCity());
            //场馆
            userClassDetailsDto.setTicketAddressVenue(ticketDetails.getTicketAddressVenue());

            //查询时间
            List<TicketDetailsTicketTimeMiddle> timeMiddle = ticketDetailsTicketTimeMiddleService.queryByTicketDetailsId(ticketDetailsId);
            //遍历集合
            ArrayList<Date> dates = new ArrayList<>();
            for (TicketDetailsTicketTimeMiddle middle : timeMiddle) {
                Long timeMiddleId = middle.getTicketTimeMiddleId();
                TicketTime ticketTime = ticketTimeService.queryById(timeMiddleId);
                dates.add(ticketTime.getTicketTime());
            }
            dates.sort((d1, d2) -> {
                return d1.compareTo(d2);
            });
            userClassDetailsDto.setTicketStartTime(dates.get(0));
            userClassDetailsDto.setTicketEndTime(dates.get(dates.size() - 1));

            //价格中间表
            List<TicketDetailsTicketPriceMiddle> priceMiddles = ticketDetailsTicketPriceMiddleService.queryByTicketDetailsId(ticketDetailsId);
            ArrayList<Double> price = new ArrayList<>();
            for (TicketDetailsTicketPriceMiddle priceMiddle : priceMiddles) {
                Long priceMiddleId = priceMiddle.getTicketPriceMiddleId();
                TicketPrice ticketPrice = ticketPriceService.queryById(priceMiddleId);
                price.add(Double.valueOf((Double) ticketPrice.getTicketPricePrice()));
            }
            price.sort((p1, p2) -> {
                return p1.compareTo(p2);
            });
            userClassDetailsDto.setTicketPricePrice(price.get(0));
            userClassDetailsDtos.add(userClassDetailsDto);
        }
        return userClassDetailsDtos;
    }

    @Override
    public List<UserClassDetailsDto> userClassDetailsDtoList2() {
        ArrayList<UserClassDetailsDto> userClassDetailsDtos = new ArrayList<>();
        try {
            List<TicketDetails> ticketDetailsList = ticketDetailsService.queryManyByWeightUp(0, 6);
            //将所有要取出的数据封装到一个集合中

            //遍历集合得到查到的票
            for (TicketDetails ticketDetails : ticketDetailsList) {
                //创建Dto对象
                UserClassDetailsDto userClassDetailsDto = new UserClassDetailsDto();
                //拿到id
                Long ticketDetailsId = ticketDetails.getTicketDetailsId();
                //设置Dto对象,id,商品名,图片地址
                userClassDetailsDto.setTicketDetailsId(ticketDetailsId);
                userClassDetailsDto.setTicketDetailsInfo(ticketDetails.getTicketDetailsInfo());
                userClassDetailsDto.setTicketDetailsTicketImgUrl(ticketDetails.getTicketDetailsTicketImgUrl());
                //城市
                userClassDetailsDto.setTicketAddressCity(ticketDetails.getTicketAddressCity());
                //场馆
                userClassDetailsDto.setTicketAddressVenue(ticketDetails.getTicketAddressVenue());
                //地点,价格 ,时间都需要关联表来查询
                //所以要建立中间表查询集合
//            List<TicketDetailsTicketAddressMiddle> ticketDetailsTicketAddressMiddles = ticketDetailsTicketAddressMiddleService.queryByTicketDetailsId(ticketDetailsId);
//            for (TicketDetailsTicketAddressMiddle t : ticketDetailsTicketAddressMiddles) {
//                //得到中间表的id
//                Long addressMiddleId = t.getTicketAddressMiddleId();
//                TicketAddress ticketAddress = ticketAddressService.queryById(ticketDetailsId);
//                userClassDetailsDto.setTicketAddressVenue(ticketAddress.getTicketAddressVenue());
//                userClassDetailsDto.setTicketAddressCity(ticketAddress.getTicketAddressCity());
//                break;
//            }

                //查询时间
                List<TicketDetailsTicketTimeMiddle> timeMiddle = ticketDetailsTicketTimeMiddleService.queryByTicketDetailsId(ticketDetailsId);
                //遍历集合
                ArrayList<Date> dates = new ArrayList<>();
                for (TicketDetailsTicketTimeMiddle middle : timeMiddle) {
                    Long timeMiddleId = middle.getTicketTimeMiddleId();
                    TicketTime ticketTime = ticketTimeService.queryById(timeMiddleId);
                    dates.add(ticketTime.getTicketTime());
                }
                dates.sort((d1, d2) -> {
                    return d1.compareTo(d2);
                });
                userClassDetailsDto.setTicketStartTime(dates.get(0));
                userClassDetailsDto.setTicketEndTime(dates.get(dates.size() - 1));

                //价格中间表
                List<TicketDetailsTicketPriceMiddle> priceMiddles = ticketDetailsTicketPriceMiddleService.queryByTicketDetailsId(ticketDetailsId);
                ArrayList<Double> price = new ArrayList<>();
                for (TicketDetailsTicketPriceMiddle priceMiddle : priceMiddles) {
                    Long priceMiddleId = priceMiddle.getTicketPriceMiddleId();
                    TicketPrice ticketPrice = ticketPriceService.queryById(priceMiddleId);
                    price.add(Double.valueOf((Double) ticketPrice.getTicketPricePrice()));
                }
                price.sort((p1, p2) -> {
                    return p1.compareTo(p2);
                });
                userClassDetailsDto.setTicketPricePrice(price.get(0));
                userClassDetailsDtos.add(userClassDetailsDto);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return userClassDetailsDtos;
    }

    @Override
    public List<UserClassDetailsDto> userClassDetailsDtoList3() {
        ArrayList<UserClassDetailsDto> userClassDetailsDtos = new ArrayList<>();
        try {
            List<TicketDetails> ticketDetailsList = ticketDetailsService.queryManyByWeightAndCreateTime(0, 6);
            //将所有要取出的数据封装到一个集合中

            //遍历集合得到查到的票
            for (TicketDetails ticketDetails : ticketDetailsList) {
                //            //创建Dto对象
                UserClassDetailsDto userClassDetailsDto = new UserClassDetailsDto();
                //拿到id
                Long ticketDetailsId = ticketDetails.getTicketDetailsId();
                //设置Dto对象,id,商品名,图片地址
                userClassDetailsDto.setTicketDetailsId(ticketDetailsId);
                userClassDetailsDto.setTicketDetailsInfo(ticketDetails.getTicketDetailsInfo());
                userClassDetailsDto.setTicketDetailsTicketImgUrl(ticketDetails.getTicketDetailsTicketImgUrl());
                //城市
                userClassDetailsDto.setTicketAddressCity(ticketDetails.getTicketAddressCity());
                //场馆
                userClassDetailsDto.setTicketAddressVenue(ticketDetails.getTicketAddressVenue());
                //地点,价格 ,时间都需要关联表来查询
                //所以要建立中间表查询集合
//            List<TicketDetailsTicketAddressMiddle> ticketDetailsTicketAddressMiddles = ticketDetailsTicketAddressMiddleService.queryByTicketDetailsId(ticketDetailsId);
//            for (TicketDetailsTicketAddressMiddle t : ticketDetailsTicketAddressMiddles) {
//                //得到中间表的id
//                Long addressMiddleId = t.getTicketAddressMiddleId();
//                TicketAddress ticketAddress = ticketAddressService.queryById(ticketDetailsId);
//                userClassDetailsDto.setTicketAddressVenue(ticketAddress.getTicketAddressVenue());
//                userClassDetailsDto.setTicketAddressCity(ticketAddress.getTicketAddressCity());
//                break;
//            }

                //查询时间
                List<TicketDetailsTicketTimeMiddle> timeMiddle = ticketDetailsTicketTimeMiddleService.queryByTicketDetailsId(ticketDetailsId);
                //遍历集合
                ArrayList<Date> dates = new ArrayList<>();
                for (TicketDetailsTicketTimeMiddle middle : timeMiddle) {
                    Long timeMiddleId = middle.getTicketTimeMiddleId();
                    TicketTime ticketTime = ticketTimeService.queryById(timeMiddleId);
                    dates.add(ticketTime.getTicketTime());
                }
                dates.sort((d1, d2) -> {
                    return d1.compareTo(d2);
                });
                userClassDetailsDto.setTicketStartTime(dates.get(0));
                userClassDetailsDto.setTicketEndTime(dates.get(dates.size() - 1));

                //价格中间表
                List<TicketDetailsTicketPriceMiddle> priceMiddles = ticketDetailsTicketPriceMiddleService.queryByTicketDetailsId(ticketDetailsId);
                ArrayList<Double> price = new ArrayList<>();
                for (TicketDetailsTicketPriceMiddle priceMiddle : priceMiddles) {
                    Long priceMiddleId = priceMiddle.getTicketPriceMiddleId();
                    TicketPrice ticketPrice = ticketPriceService.queryById(priceMiddleId);
                    price.add(Double.valueOf((Double) ticketPrice.getTicketPricePrice()));
                }
                price.sort((p1, p2) -> {
                    return p1.compareTo(p2);
                });
                userClassDetailsDto.setTicketPricePrice(price.get(0));
                userClassDetailsDtos.add(userClassDetailsDto);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return userClassDetailsDtos;
    }

    @Override
    public List<UserClassDetailsDto> userClassDetailsDtoList4() {
        List<TicketDetails> ticketDetailsList = ticketDetailsService.queryManyByWeightAndCreateTimeUp(0, 6);
        //将所有要取出的数据封装到一个集合中
        ArrayList<UserClassDetailsDto> userClassDetailsDtos = new ArrayList<>();
        //遍历集合得到查到的票
        for (TicketDetails ticketDetails : ticketDetailsList) {
            //            //创建Dto对象
            UserClassDetailsDto userClassDetailsDto = new UserClassDetailsDto();
            //拿到id
            Long ticketDetailsId = ticketDetails.getTicketDetailsId();
            //设置Dto对象,id,商品名,图片地址
            userClassDetailsDto.setTicketDetailsId(ticketDetailsId);
            userClassDetailsDto.setTicketDetailsInfo(ticketDetails.getTicketDetailsInfo());
            userClassDetailsDto.setTicketDetailsTicketImgUrl(ticketDetails.getTicketDetailsTicketImgUrl());
            //城市
            userClassDetailsDto.setTicketAddressCity(ticketDetails.getTicketAddressCity());
            //场馆
            userClassDetailsDto.setTicketAddressVenue(ticketDetails.getTicketAddressVenue());
            //地点,价格 ,时间都需要关联表来查询
            //所以要建立中间表查询集合
//            List<TicketDetailsTicketAddressMiddle> ticketDetailsTicketAddressMiddles = ticketDetailsTicketAddressMiddleService.queryByTicketDetailsId(ticketDetailsId);
//            for (TicketDetailsTicketAddressMiddle t : ticketDetailsTicketAddressMiddles) {
//                //得到中间表的id
//                Long addressMiddleId = t.getTicketAddressMiddleId();
//                TicketAddress ticketAddress = ticketAddressService.queryById(ticketDetailsId);
//                userClassDetailsDto.setTicketAddressVenue(ticketAddress.getTicketAddressVenue());
//                userClassDetailsDto.setTicketAddressCity(ticketAddress.getTicketAddressCity());
//                break;
//            }

            //查询时间
            List<TicketDetailsTicketTimeMiddle> timeMiddle = ticketDetailsTicketTimeMiddleService.queryByTicketDetailsId(ticketDetailsId);
            //遍历集合
            ArrayList<Date> dates = new ArrayList<>();
            for (TicketDetailsTicketTimeMiddle middle : timeMiddle) {
                Long timeMiddleId = middle.getTicketTimeMiddleId();
                TicketTime ticketTime = ticketTimeService.queryById(timeMiddleId);
                dates.add(ticketTime.getTicketTime());
            }
            dates.sort((d1, d2) -> {
                return d1.compareTo(d2);
            });
            userClassDetailsDto.setTicketStartTime(dates.get(0));
            userClassDetailsDto.setTicketEndTime(dates.get(dates.size() - 1));

            //价格中间表
            List<TicketDetailsTicketPriceMiddle> priceMiddles = ticketDetailsTicketPriceMiddleService.queryByTicketDetailsId(ticketDetailsId);
            ArrayList<Double> price = new ArrayList<>();
            for (TicketDetailsTicketPriceMiddle priceMiddle : priceMiddles) {
                Long priceMiddleId = priceMiddle.getTicketPriceMiddleId();
                TicketPrice ticketPrice = ticketPriceService.queryById(priceMiddleId);
                price.add(Double.valueOf((Double) ticketPrice.getTicketPricePrice()));
            }
            price.sort((p1, p2) -> {
                return p1.compareTo(p2);
            });
            userClassDetailsDto.setTicketPricePrice(price.get(0));
            userClassDetailsDtos.add(userClassDetailsDto);
        }
        return userClassDetailsDtos;
    }

    @Override
    public PageInfo<UserClassDetailsDto> queryTicketDetailsByCenter(Integer pageNum, Integer pageSize) {
        if (null == pageNum || null == pageSize) {
            pageNum = 0;
            pageSize = 6;
        }
        // 写在查询的前面
        PageHelper.startPage(pageNum, pageSize);
        // 查询
        List<UserClassDetailsDto> userClassDetailsDtos = this.ticketDetailsService.queryTicketDetailsByCenter();
        // 把查询的结果封装到 PageInfo
        PageInfo<UserClassDetailsDto> ticketDetailsPageInfo = new PageInfo<>(userClassDetailsDtos);

        return ticketDetailsPageInfo;
    }

    @Override
    public PageInfo<UserClassDetailsDto> queryTicketDetailsByCenterUp(Integer pageNum, Integer pageSize) {
        if (null == pageNum || null == pageSize) {
            pageNum = 0;
            pageSize = 6;
        }
        // 写在查询的前面
        PageHelper.startPage(pageNum, pageSize);
        // 查询
        List<UserClassDetailsDto> userClassDetailsDtos = this.ticketDetailsService.queryTicketDetailsByCenterUp();
        PageInfo<UserClassDetailsDto> userClassDetailsDtoPageInfo = new PageInfo<>(userClassDetailsDtos);
        return userClassDetailsDtoPageInfo;
    }

    @Override
    public PageInfo<UserClassDetailsDto> queryTicketDetailsByCenterCreatTime(Integer pageNum, Integer pageSize) {
        if (null == pageNum || null == pageSize) {
            pageNum = 0;
            pageSize = 6;
        }
        // 写在查询的前面
        PageHelper.startPage(pageNum, pageSize);
        // 查询
        List<UserClassDetailsDto> userClassDetailsDtos = this.ticketDetailsService.queryTicketDetailsByCenterCreatTime();
        PageInfo<UserClassDetailsDto> userClassDetailsDtoPageInfo = new PageInfo<>(userClassDetailsDtos);
        return userClassDetailsDtoPageInfo;
    }

    @Override
    public PageInfo<UserClassDetailsDto> queryTicketDetailsByCenterCreatTimeDown(Integer pageNum, Integer pageSize) {
        if (null == pageNum || null == pageSize) {
            pageNum = 0;
            pageSize = 6;
        }
        // 写在查询的前面
        PageHelper.startPage(pageNum, pageSize);
        // 查询
        List<UserClassDetailsDto> userClassDetailsDtos = this.ticketDetailsService.queryTicketDetailsByCenterCreatTimeDown();
        PageInfo<UserClassDetailsDto> userClassDetailsDtoPageInfo = new PageInfo<>(userClassDetailsDtos);
        return userClassDetailsDtoPageInfo;
    }
}
